package es.osoco.basicpomodoro.timer;

import es.osoco.basicpomodoro.Main;
import es.osoco.basicpomodoro.R;
import es.osoco.basicpomodoro.preferences.PomodoroPreferences;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;

public class PomodoroService extends Service {
	private long startTime;
	private long endTime;
	private String notificationTitle;
	private String notificationSummary;
	private IBinder binder = new ServiceBinder();
	private Handler handler = new Handler();
	private Runnable timer = new Runnable () {
		public void run() {
			Log.d("START TIME",startTime+"");
			Log.d("END TIME",endTime+"");
			Log.d("CURRENT TIME",System.currentTimeMillis()+"");
			if(endTime <= System.currentTimeMillis()) {
				             
                int icon = R.drawable.pomodoro;
                CharSequence tickerText = notificationTitle;
                long when = System.currentTimeMillis();
                Context context = getApplicationContext();
                CharSequence contentTitle = notificationTitle;
                CharSequence contentText = notificationSummary;

                Intent notificationIntent = new Intent(context, Main.class);
                PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                
                Notification notification = new Notification(icon, tickerText, when);
                notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                notification.
                
                String ns = Context.NOTIFICATION_SERVICE;
                NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
                mNotificationManager.notify(1, notification);
                
				handler.removeCallbacks(timer);
			}
			else
			{
				handler.postDelayed(timer,1000);
			}
		}
	};
	
	@Override
	public void onCreate() {

	}
	
	@Override
	public IBinder onBind(Intent intent) {
		Log.d("ON BIND","OK");
		return binder;
	}
	
	public class ServiceBinder extends Binder {
		public PomodoroService getService() {
			return PomodoroService.this;
		}
	}

	public void startPomodoro(long minutes) {
		loadConfig();
		startTime = System.currentTimeMillis();
		endTime = startTime + (minutes * 60000);
		handler.removeCallbacks(timer);
		handler.post(timer);
	}
	
	private void loadConfig() {
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		notificationTitle = sharedPreferences.getString(PomodoroPreferences.KEY_NOTIFICATION_TITLE, "Pomodoro completado");
		notificationSummary = sharedPreferences.getString(PomodoroPreferences.KEY_NOTIFICATION_SUMMARY, "Ya puedes descansar");
	}
}
